<template>
    <div class="content-container">
        <div class="per-header">
            <el-button type="success" icon="el-icon-refresh" @click="reloadPermission()">重新加载</el-button>
        </div>
        <el-table :data="permissions" border stripe>
            <el-table-column prop="id" label="编号"></el-table-column>
            <el-table-column prop="name" label="权限名称"></el-table-column>
            <el-table-column prop="expression" label="权限表达式"></el-table-column>
            <el-table-column prop="id" label="操作" width="100px">
                <template slot-scope="scope">
                    <el-button type="danger" icon="el-icon-delete" @click="delPermission(scope.row)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="content-footer">
            <el-pagination
                    @size-change="sizeChange"
                    @current-change="currentChange"
                    background
                    :page-size="pageSize"
                    :page-sizes="[5, 10, 50, 100]"
                    :total="total"
                    layout="sizes,prev,pager,next,total"
            >
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import {getPermissionsApi, delPermissionsApi, reloadPermissionsApi} from '@/request/api'

    export default {
        name: "Permission",
        data() {
            return {
                permissions: [],
                currentPage: 1,
                pageSize: 10,
                total: 0,
            }
        },
        methods: {
            sizeChange(size) {
                this.pageSize = size
                this.queryPermissionsData()
            },
            currentChange(page) {
                this.currentPage = page
                this.queryPermissionsData()
            },
            queryPermissionsData() {
                getPermissionsApi({
                    currentPage: this.currentPage,
                    pageSize: this.pageSize
                }).then(res => {
                    if (res.success === true) {
                        this.permissions = res.data.list
                        this.total = res.data.total
                    }
                })
            },
            // 删除某一个权限
            delPermission(row) {
                this.$confirm('此操作将删除该权限，是否继续？', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    delPermissionsApi({
                        id: row.id
                    }).then(res => {
                        if (res.success === true) {
                            this.$message.success('删除成功!')
                            this.queryPermissionsData()
                        }
                    })
                }).catch(() => {

                })
            },
            // 重新加载权限
            reloadPermission() {
                this.$confirm('此操作将重新加载权限，是否继续？', '操作提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    reloadPermissionsApi({
                    }).then(res => {
                        if (res.success=== true) {
                            this.$message.success('加载成功！')
                            this.queryPermissionsData()
                        }
                    })
                }).catch(() => {

                })

            }
        },
        created() {
            this.queryPermissionsData()
        }
    }
</script>

<style lang="less" type="text/less" scoped>
    .per-header {
        margin-bottom: 10px;
    }
</style>